﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SilverTile.Sprites
{
    public class AnimatedSprite : Sprite
    {

        protected Dictionary<string, List<IFrame>> _animations;
        private List<IFrame> _frames;
        private TimeSpan _timePerFrame = TimeSpan.FromMilliseconds(100);

        private TimeSpan _frameTime = TimeSpan.Zero;

        private int _frameIdx = 0;

        private string _activeAnimation;
        private Action<AnimatedSprite> _animationCompleteMethod = null;

        public AnimatedSprite(string animationName, params IFrame[] frames)
            : base(frames[0])
        {

            _animations = new Dictionary<string, List<IFrame>>();
            AddAnimations(animationName, frames);
            SetActiveAnimation(animationName);

        }

        public AnimatedSprite(int width, int height) : base(width, height)
        {
            _animations = new Dictionary<string, List<IFrame>>();
        }

        public int Fps {
            get { return (int)(1000 / _timePerFrame.TotalMilliseconds); }
            set { _timePerFrame = TimeSpan.FromMilliseconds(1000 / value); }
        }

        public override void Update(TimeSpan timeSinceLastFrame)
        {

            bool animationComplete = false;

            _frameTime += timeSinceLastFrame;
            while (_frameTime > _timePerFrame)
            {

                _frameTime -= _timePerFrame;

                _frameIdx++;
                if (_frameIdx >= _frames.Count)
                {
                    animationComplete = true;
                    _frameIdx = 0;
                }
                _frame = _frames[_frameIdx];

            }

            if (animationComplete && _animationCompleteMethod != null)
            {
                var method = _animationCompleteMethod;
                _animationCompleteMethod = null;
                method(this);                
            }
            
            base.Update(timeSinceLastFrame);
        }


        public void AddAnimations(string animationName, params IFrame[] frames)
        {
            _animations.Add(animationName, new List<IFrame>(frames));
            if (_animations.Count == 1)
            {
                // Set the active animation to the first animation we add
                SetActiveAnimation(animationName);
            }
        }


        public void SetActiveAnimation(string animationName)
        {
            SetActiveAnimation(animationName, null);
        }

        public void SetActiveAnimation(string animationName, Action<AnimatedSprite> animationCompleteMethod)
        {
            try
            {
                _frames = _animations[animationName];
                _frame = _frames[0];
                _frameIdx = 0;
                _activeAnimation = animationName;
                _animationCompleteMethod = animationCompleteMethod;
            }
            catch (KeyNotFoundException keyEx)
            {
                throw new ArgumentOutOfRangeException("Unknown animation: " + animationName, keyEx);
            }

        }

        public string ActiveAnimation
        {
            get { return _activeAnimation; }
        }

        internal Dictionary<string, List<IFrame>> Animations
        {
            get { return _animations; }
        }

    }

}
